package com.zbbt.sportslottery.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.bean.DialogBean;
import com.zbbt.sportslottery.bean.DialogBean.OnDialogClickListener;
import com.zbbt.sportslottery.util.ViewUtil;

public class SimpleCustomDialog extends Dialog {

	public SimpleCustomDialog(Context context) {
		super(context);
	}

	public SimpleCustomDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder implements android.view.View.OnClickListener {
		public static DialogBean dialogBean;

		private TextView tv_title;// 标题

		private TextView tv_left;// 左边按钮

		private TextView tv_right;// 右边按钮

		private View mView;// 扩展View

		private LinearLayout ll_container;// 扩展view的容器

		private int layoutId;

		private Context context;

		public SimpleCustomDialog dialog;

		public Builder(Context context) {
			this.context = context;
		}

		public SimpleCustomDialog create() {
			int style = dialogBean.getStyle();
			switch (style) {
			case DialogBean.STYLE_BTN:
				layoutId = R.layout.dialog_custom_style_btn;
				break;
			default:
				layoutId = R.layout.dialog_custom_style_btn;
				break;
			}
			// 加载通用布局
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View dialogView = inflater.inflate(layoutId, null);
			tv_title = (TextView) dialogView.findViewById(R.id.tv_title);
			tv_left = (TextView) dialogView.findViewById(R.id.tv_left);
			tv_right = (TextView) dialogView.findViewById(R.id.tv_right);
			ll_container = (LinearLayout) dialogView.findViewById(R.id.ll_container);

			// 填充扩展布局
			mView = dialogBean.getContentView();
			ll_container.removeAllViews();
			if (mView != null) {
				ll_container.addView(getContentView(mView));
			}

			// 设置dialog样式
			dialog = new SimpleCustomDialog(context, R.style.custom_dialog);
			// 设置dialog整体布局
			dialog.addContentView(dialogView, new LayoutParams(ViewUtil.getDialogWidth(), LayoutParams.WRAP_CONTENT));
			// dialog.setContentView(dialogView);

			// 设置数据信息
			if (TextUtils.isEmpty(dialogBean.getTitle())) {
				tv_title.setVisibility(View.GONE);
			} else {
				tv_title.setText(dialogBean.getTitle());
			}
			CharSequence lbtText = dialogBean.getLbtText();
			CharSequence rbtText = dialogBean.getRbtText();
			if ("".equals(lbtText)) {
				tv_left.setVisibility(View.GONE);
			} else {
				tv_left.setText(lbtText);
			}
			if ("".equals(rbtText)) {
				tv_right.setVisibility(View.GONE);
			} else {
				tv_right.setText(rbtText);
			}
			// 设置监听
			setListener();
			return dialog;
		}

		private View getContentView(View mView) {
			if (mView instanceof ViewGroup) {
				if (mView.getLayoutParams() == null) {
					LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT);
					mView.setLayoutParams(params);
				}
			}
			return mView;
		}

		private void setListener() {
			tv_left.setOnClickListener(this);
			tv_right.setOnClickListener(this);
		}

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.tv_left:
				OnDialogClickListener lbtnListener = dialogBean.getLbtnListener();
				if (lbtnListener == null) {
					dialog.dismiss();
				} else {
					if (!lbtnListener.onClick(v)) {
						dialog.dismiss();
					}
				}
				break;
			case R.id.tv_right:
				OnDialogClickListener rbtnListener = dialogBean.getRbtnListener();
				if (rbtnListener == null) {
					dialog.dismiss();
				} else {
					if (!rbtnListener.onClick(v)) {
						dialog.dismiss();
					}
				}
				break;

			default:
				break;
			}
		}

	}
}
